package com.czl.module_service.fragment.assetHandle

import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.alibaba.android.arouter.facade.annotation.Route
import com.czl.base.base.BaseFragment
import com.czl.base.config.AppConstants
import com.czl.base.event.LiveBusCenter
import com.czl.module_service.BR
import com.czl.module_service.R
import com.czl.module_service.adapter.AssetHandleFacilityInuseAdapter
import com.czl.module_service.databinding.FragmentAssetHandleFacilityDamageBinding
import com.czl.module_service.extension.loadCompletedData
import com.czl.module_service.view.ListSheetDialogFragment
import com.czl.module_service.viewmodel.AssetHandleFacilityDamageViewModel

@Route(path = AppConstants.Router.Service.F_SERVICE_ASSET_HANDLE_FACILITY_DAMAGE)
class AssetHandleFacilityDamageFragment : BaseFragment<FragmentAssetHandleFacilityDamageBinding, AssetHandleFacilityDamageViewModel>() {

    private lateinit var mAdapter: AssetHandleFacilityInuseAdapter

    override fun initContentView(): Int {
        return R.layout.fragment_asset_handle_facility_damage
    }

    override fun initVariableId(): Int {
        return BR.viewModel
    }

    override fun initData() {
        super.initData()
        initAdapter()
        reload()
    }

    override fun useBaseLayout(): Boolean {
        return false
    }

    private fun initAdapter() {
        mAdapter = AssetHandleFacilityInuseAdapter().apply {
        }
        binding.ryCommon.apply{
            layoutManager = LinearLayoutManager(context, RecyclerView.VERTICAL, false)
            adapter = mAdapter
        }
        mAdapter.facilityIdList =
            arguments?.getIntegerArrayList("facilityIdList") as ArrayList<Int>
        viewModel.parentCompanyId = arguments?.getSerializable("parentCompanyId") as Int
    }

    override fun initViewObservable() {
        viewModel.uc.loadCompleteEvent.observe(this, { data ->
            this.loadCompletedData(
                data?.hasNextPage,
                viewModel.currentPage == 2,
                data != null,
                data?.total,
                data?.list,
                mAdapter,
                binding.ryCommon,
                binding.smartCommon
            )
        })
        viewModel.uc.loadChangeEvent.observe(this, { data ->
            if (data == 0) {
                viewModel.currentPage = 1
                getDataInfo()
            } else if (data == 1) {
                ListSheetDialogFragment.showBottomSheet(
                    parentFragmentManager,
                    viewModel.storehouseNameList
                ) {
                    viewModel.storehouseInfo.set(viewModel.storehouseNameList[it])
                    var comBean = viewModel.sStorehouseList[it]
                    viewModel.storehouseId = comBean.storehouseId
                    viewModel.currentPage = 1
                    getDataInfo()
                }
            }
        })
        viewModel.uc.loadSubmitEvent.observe(this, { data ->
            LiveBusCenter.postAddAssetHanldeFacilityEvent(data)
        })
    }

    fun getDataInfo () {
        mAdapter.setList(arrayListOf())
        viewModel.getDataList()
    }

    override fun reload() {
//        binding.smartCommon.autoRefresh()
        viewModel.getStorehouseList()
    }
}